package com.veo.service.Iml;

import com.veo.dao.OrderMapper;
import com.veo.domain.Order;
import com.veo.service.AccountService;
import com.veo.service.OrderService;
import com.veo.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService{

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private StorageService storageService;

    @Resource
    private AccountService accountService;

    @Override
    //@GlobalTransactional(name = "veo-create-order",rollbackFor = Exception.class)
    public void create(Order order) {
        //1.新建订单
        log.info(">>>>>>>>>>>>开始新建订单");
        orderMapper.create(order);

        //2.扣减库存
        log.info(">>>>>>>>>>>>订单微服务开始调库存微服务，做扣减Count");
        storageService.decrease(order.getProductId(),order.getCount());
        log.info(">>>>>>>>>>>>订单微服务开始调库存微服务，做扣减CountEnd");

        //3.扣减账户
        log.info(">>>>>>>>>>>>订单微服务开始调库存微服务，做扣减Money");
        accountService.decrease(order.getUserId(),order.getMoney());
        log.info(">>>>>>>>>>>>订单微服务开始调库存微服务，做扣减MoneyEnd");

        //4.修改订单状态
        log.info(">>>>>>>>>>>>修改订单状态开始");
        orderMapper.update(order.getUserId(),0);
        log.info(">>>>>>>>>>>>下订单结束O(∩_∩)O哈哈~");
    }
}
